package center

import (
	"base"
	"leaf/gate"
	"login_server/chanrpc"
	. "login_server/logic/internal"
	"proto/base"
	"proto/server/center"
)

type centerSystem struct {
	agent gate.Agent
}

var system = new(centerSystem)

func init() {
	Skeleton.AfterInit(func() {
		Skeleton.RegisterChanRPC(chanrpc.NewCenterAgent, system.newAgent)
		Skeleton.RegisterChanRPC(chanrpc.CloseCenterAgent, system.closeAgent)
	})
}

func (*centerSystem) newAgent(args []interface{}) {
	agent := args[0].(gate.Agent)
	base.Logger.Debug("new center agent, remote address: %s, local address: %s", agent.RemoteAddr(), agent.LocalAddr())

	system.agent = agent

	msg := new(proto_server_center.Register)
	msg.Server = proto_base.Server_LoginServer
	rpc.Register(msg)
}

func (*centerSystem) closeAgent(args []interface{}) {
	agent := args[0].(gate.Agent)
	base.Logger.Debug("close center agent, remote address: %s, local address: %s", agent.RemoteAddr(), agent.LocalAddr())

	system.agent = nil
}
